package com.pfizer.pfizerExpertForPad;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceChangeListener;
import android.view.KeyEvent;

public class ActivitySetting extends PreferenceActivity implements
		OnPreferenceChangeListener {

	SharedPreferences spSettings;
	SharedPreferences.Editor spEditor;
	EditTextPreference etWebUrl;
	EditTextPreference etLocalFilePath;
	ListPreference lpVideoUpdIntervalTime;
	ListPreference lpHandoutUpdIntervalTime;
	ListPreference lpSurveyUpdIntervalTime;
	ListPreference lpNewsUpdIntervalTime;
	ListPreference lpMsgUpdIntervalTime;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// requestWindowFeature(Window.FEATURE_NO_TITLE);
		try {
			setContentView(R.layout.lo_setting);
			addPreferencesFromResource(R.xml.preference_setting);
			String prefsName = getPackageName() + "_preferences";
			spSettings = getSharedPreferences(prefsName,
					Context.MODE_WORLD_WRITEABLE);
			spEditor = spSettings.edit();
			etWebUrl = (EditTextPreference) findPreference("etWebUrl");
			etWebUrl.setOnPreferenceChangeListener(this);
			etLocalFilePath = (EditTextPreference) findPreference("etLocalFilePath");
			etLocalFilePath.setOnPreferenceChangeListener(this);
			lpVideoUpdIntervalTime = (ListPreference) findPreference("lpVideoUpdIntervalTime");
			lpVideoUpdIntervalTime.setOnPreferenceChangeListener(this);
			lpHandoutUpdIntervalTime = (ListPreference) findPreference("lpHandoutUpdIntervalTime");
			lpHandoutUpdIntervalTime.setOnPreferenceChangeListener(this);
			lpSurveyUpdIntervalTime = (ListPreference) findPreference("lpSurveyUpdIntervalTime");
			lpSurveyUpdIntervalTime.setOnPreferenceChangeListener(this);
			lpNewsUpdIntervalTime = (ListPreference) findPreference("lpNewsUpdIntervalTime");
			lpNewsUpdIntervalTime.setOnPreferenceChangeListener(this);
			lpMsgUpdIntervalTime = (ListPreference) findPreference("lpMsgUpdIntervalTime");
			lpMsgUpdIntervalTime.setOnPreferenceChangeListener(this);
			InitPreference();
		} catch (Exception ex) {
			return;
		}
	}

	private void InitPreference() {
		String sTmp = "";
		int iTmp = 0;
		sTmp = spSettings.getString("etWebUrl",
				"http://www.testing.htm.tw");
		etWebUrl.setSummary(sTmp);
		sTmp = spSettings.getString("etLocalFilePath", "/sdcard/pfizer/");
		etLocalFilePath.setSummary(sTmp);
		sTmp = spSettings.getString("lpVideoUpdIntervalTime", "6");
		iTmp = new Integer(sTmp).intValue();
		lpVideoUpdIntervalTime.setSummary(String.format("间隔%d小时", iTmp / 6));
		sTmp = spSettings.getString("lpHandoutUpdIntervalTime", "6");
		iTmp = new Integer(sTmp).intValue();
		lpHandoutUpdIntervalTime.setSummary(String.format("间隔%d小时", iTmp / 6));
		sTmp = spSettings.getString("lpSurveyUpdIntervalTime", "6");
		iTmp = new Integer(sTmp).intValue();
		lpSurveyUpdIntervalTime.setSummary(String.format("间隔%d小时", iTmp / 6));
		sTmp = spSettings.getString("lpNewsUpdIntervalTime", "6");
		iTmp = new Integer(sTmp).intValue();
		lpNewsUpdIntervalTime.setSummary(String.format("间隔%d小时", iTmp / 6));
		sTmp = spSettings.getString("lpMsgUpdIntervalTime", "4");
		iTmp = new Integer(sTmp).intValue();
		lpMsgUpdIntervalTime.setSummary(String.format("间隔%d分钟", iTmp));
	}

	@Override
	public boolean onPreferenceChange(Preference preference, Object newValue) {
		// TODO Auto-generated method stub
		String pfKey = preference.getKey();
		if (pfKey.equals("etWebUrl")) {
			etWebUrl.setText(String.valueOf(newValue));
			spEditor.commit();
			etWebUrl.setSummary(String.valueOf(newValue));
		} else if (pfKey.equals("etLocalFilePath")) {
			etLocalFilePath.setText(String.valueOf(newValue));
			spEditor.commit();
			etLocalFilePath.setSummary(String.valueOf(newValue));
		} else if (pfKey.equals("lpVideoUpdIntervalTime")) {
			lpVideoUpdIntervalTime.setValue(newValue.toString());
			spEditor.commit();
			lpVideoUpdIntervalTime.setSummary(String.format("间隔%d小时", Integer
					.parseInt(newValue.toString()) / 6));
		} else if (pfKey.equals("lpHandoutUpdIntervalTime")) {
			lpHandoutUpdIntervalTime.setValue(newValue.toString());
			spEditor.commit();
			lpHandoutUpdIntervalTime.setSummary(String.format("间隔%d小时", Integer
					.parseInt(newValue.toString()) / 6));
		} else if (pfKey.equals("lpSurveyUpdIntervalTime")) {
			lpSurveyUpdIntervalTime.setValue(newValue.toString());
			spEditor.commit();
			lpSurveyUpdIntervalTime.setSummary(String.format("间隔%d小时", Integer
					.parseInt(newValue.toString()) / 6));
		} else if (pfKey.equals("lpNewsUpdIntervalTime")) {
			lpNewsUpdIntervalTime.setValue(newValue.toString());
			spEditor.commit();
			lpNewsUpdIntervalTime.setSummary(String.format("间隔%d小时", Integer
					.parseInt(newValue.toString()) / 6));
		} else if (pfKey.equals("lpMsgUpdIntervalTime")) {
			lpMsgUpdIntervalTime.setValue(newValue.toString());
			spEditor.commit();
			lpMsgUpdIntervalTime.setSummary(String.format("间隔%d分钟", Integer
					.parseInt(newValue.toString())));
		}
		return false;
	}

	@Override
	// 监听退出键
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			InitApp initApp = new InitApp(this);
			initApp.SetApp(1);
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}
}